package TCP.Case;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class CommunicationThread extends Thread{
    private Socket socket;
    CommunicationThread(){}
   public CommunicationThread(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        System.out.println("与"+socket.getRemoteSocketAddress()+"建立成功...");
        try {
            InputStream inputStream = socket.getInputStream();//从socket管道中获得一个字节输入流
            BufferedReader bufferedReader = new BufferedReader
                    (new InputStreamReader(inputStream));//获得缓冲字符输入流 更高效 这里要借助字符转化流
            String msg="";
            while ((msg=bufferedReader.readLine())!=null){
                System.out.println(socket.getRemoteSocketAddress()+"说了："+msg);
            }
            System.out.println(socket.getRemoteSocketAddress()+"走了...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
